package com.kh.statistics.video.core.login;

import com.kh.statistics.video.constant.PlatformConstant;
import com.kh.statistics.video.core.login.entity.ShiPinHaoHealthCheck;
import com.kh.statistics.video.core.login.entity.ShipinHaoHealthCheckResponse;
import com.kh.statistics.video.entity.StatisticsMeta;
import com.kh.statistics.video.entity.VideoAccountsHealth;
import com.kh.statistics.video.entity.VideoAccountsLogin;
import com.kh.statistics.video.helper.CookieHelper;
import com.kh.statistics.video.helper.HttpServletHelper;
import com.kh.statistics.video.helper.StatisticsHttpHelper;
import com.kh.statistics.video.helper.VideoAccountsHealthHelper;
import com.kh.statistics.video.utils.SerializationUtils;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @BelongsProject: statistics
 * @BelongsPackage: com.kh.statistics.video.core.login
 * @Author: Hong.Kong
 * @CreateTime: 2024-05-13  14:05
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class ShiPinHaoAccountsLogin implements AccountsLogin {
    @Override
    public String support() {
        return PlatformConstant.SHI_PIN_HAO;
    }

    @Override
    public Boolean login(VideoAccountsLogin videoAccountsLogin, WebDriver driver) throws IOException, InterruptedException {
        boolean flag = true;
        String content = null;
        while (flag || null == content) {
            driver.get("https://channels.weixin.qq.com/platform/login-for-iframe?dark_mode=true&host_type=1");


            Thread.sleep(4000);
            WebElement qrcode = driver.findElement(By.className("qrcode"));
            flag = false;
            content = qrcode.getAttribute("src");
        }

        content = "<img style=\"width: 400px\" src=\"" + content + "\" />";
        content = "<div>" + content + "</div>";
        System.out.println(content);
        HttpServletHelper.writer(content);
        CookieHelper.getCookies(driver, "视频号");
        CookieConvertMyCookie.setCookies(driver, videoAccountsLogin);
        WebDriver.Options manage = driver.manage();
        Set<Cookie> cookies1 = manage.getCookies();
        videoAccountsLogin.setCookieSet(CookieConvertMyCookie.CookiesToMyCookies(cookies1));
        return true;
    }

    @Override
    public VideoAccountsHealth creatorHealthChecks(VideoAccountsLogin videoAccountsLogin) {
        StatisticsMeta statisticsMeta = new StatisticsMeta();
        statisticsMeta.setRequestType("post");
        statisticsMeta.setUrl("https://channels.weixin.qq.com/cgi-bin/mmfinderassistant-bin/member/member_list");
        Map<String, String> headers = new HashMap<>();
        headers.put("cookie", videoAccountsLogin.getCookies());
        headers.put("origin", "https://channels.weixin.qq.com");
        headers.put("referer", "https://channels.weixin.qq.com/platform/setting/member");
        statisticsMeta.setHeaders(headers);
        ShiPinHaoHealthCheck shiPinHaoHealthCheck = new ShiPinHaoHealthCheck();
        shiPinHaoHealthCheck.setReqScene(7);
        shiPinHaoHealthCheck.setScene(7);
        shiPinHaoHealthCheck.setTimestamp(String.valueOf(System.currentTimeMillis()));
        shiPinHaoHealthCheck.setLogFinderId("v2_060000231003b20faec8cae3801cc3d3cd01e83cb0777cd2e4cff4b98a8148def3ff3ba41364@finder");
        shiPinHaoHealthCheck.setLogFinderUin("");
        statisticsMeta.setJsonParam(SerializationUtils.serialize(shiPinHaoHealthCheck));
        String body = StatisticsHttpHelper.request(statisticsMeta);
        ShipinHaoHealthCheckResponse response = SerializationUtils.deserialize(body, ShipinHaoHealthCheckResponse.class);
        VideoAccountsHealth videoAccountsHealth = VideoAccountsHealthHelper.getVideoAccountsHealth(videoAccountsLogin);
        videoAccountsHealth.setPlatformName("视频号");
        if (response.getErrCode() == 0) {
            videoAccountsHealth.setStatus(1);
            log.info("视频号健康检查正常：account:{},response:{}", videoAccountsHealth, response);
        } else {
            log.info("视频号健康检查异常：account:{},response:{}", videoAccountsHealth, response);
        }
        return videoAccountsHealth;
    }

    @Override
    public VideoAccountsHealth webHealthChecks(VideoAccountsLogin videoAccountsLogin) {
        return null;
    }
}
